package com.hjj.springboot.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hjj.springboot.config.LogType;
import com.hjj.springboot.config.SystemLogs;
import com.hjj.springboot.entity.*;
import com.hjj.springboot.service.IBigTypeService;
import com.hjj.springboot.service.IProductService;
import com.hjj.springboot.service.ISmallTypeService;
import com.hjj.springboot.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/smallType")
public class AdminSmallTypeController {

    @Autowired
    private ISmallTypeService smallTypeService;

    @Autowired
    private IProductService productService;

    @Autowired
    private IBigTypeService bigTypeService;

    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean) {
        Map<String, Object> map = new HashMap<>();
        map.put("name",pageBean.getQuery().trim());
        map.put("start",pageBean.getStart());
        map.put("pageSize",pageBean.getPageSize());
        List<SmallType> smallTypeList=smallTypeService.list(map);
        Long total=smallTypeService.getTotal(map);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("smallTypeList",smallTypeList);
        resultMap.put("total",total);
        return R.ok(resultMap);
    }

    @SystemLogs(operation = "商品小类信息",type = LogType.DELETE)
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id")Integer id) {
        if (productService.count(new QueryWrapper<Product>().eq("typeId", id)) > 0) {
            return R.error(500,"商品小类下有商品信息，不能直接删除");
        }else {
            smallTypeService.removeById(id);
            return R.ok();
        }
    }

    //    批量删除
    @SystemLogs(operation = "商品小类信息",type = LogType.BATCH_DELETE)
    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids) {
       if (productService.count(new QueryWrapper<Product>().in("typeId", ids)) > 0) {
           return R.error(500,"商品小类下有商品信息，不能直接删除");
       }else {
           smallTypeService.removeByIds(ids);
           return R.ok();
       }
    }

    @SystemLogs(operation = "商品小类信息",type = LogType.UPDATE)
    @RequestMapping("/save")
    public R save(@RequestBody SmallType smallType){
        smallType.setBigTypeId(smallType.getBigType().getId());
        if(smallType.getId()==null || smallType.getId()==-1){
            smallTypeService.save(smallType);
        }else {
            smallTypeService.saveOrUpdate(smallType);
        }
        return R.ok();
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable(value = "id")Integer id){
        SmallType smallType=smallTypeService.getById(id);
        smallType.setBigType(bigTypeService.getById(smallType.getBigTypeId()));
        Map<String,Object> map=new HashMap<>();
        map.put("smallType",smallType);
        return R.ok(map);
    }

//    根据商品大类查询所有小类
    @GetMapping("/listAll/{bigTypeId}")
    public R listAll(@PathVariable(value = "bigTypeId")Integer bigTypeId){
        Map<String,Object> map=new HashMap<>();
        map.put("smallTypeList",smallTypeService.list(new QueryWrapper<SmallType>().eq("bigTypeId", bigTypeId)));
        return R.ok(map);
    }

    @GetMapping("/export")
    public void export(@RequestParam(required = false) String query, HttpServletResponse response)   throws Exception {
        String queryStr=query.trim();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("商品小类信息", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<SmallType> smallTypeList = smallTypeService.list(new QueryWrapper<SmallType>().like(StringUtil.isNotEmpty(queryStr),"name",query));
        EasyExcel.write(response.getOutputStream(),SmallType.class)
                .sheet("商品小类信息")
                .doWrite(smallTypeList);
    }
}
